<template>
	<view class="address-list-area">
		<block v-for="item in addressList" :key="item.recordid">
			<shopaddress :username="item.person" :tel="item.tel" :address="item.address">
				<template v-slot:icon>
					<van-icon name="cross" @click="delCurrentAddress(item.recordid)" />
				</template>
			</shopaddress>
		</block>
		<van-button plain hairline type="primary" @click="gotoAddAddressInfo">添加新的收货地址</van-button>
	</view>
</template>

<script>
	import shopAddress from '../../components/shop-address/shop-address'
	export default {
		onLoad(val) {
			this.uid = uni.getStorageSync('uid')
			this.getUserAddressInfo()
			this.type = val.type
		},
		onShow() {
			this.getUserAddressInfo()
		},
		components: {
			shopaddress: shopAddress 
		},
		data() {
			return {
				// 用户id
				uid: '',
				// 存储该用户的地址列表信息
				addressList: [],
				// 当前被激活的单选框
				activeRadio: -1,
				// 从订单生成页跳转过来的将会把type值设定为1
				type: ''
			}
		},
		methods: {
			// 获取用户地址信息
			async getUserAddressInfo() {
				const { data: res } = await uni.$http.get('/address', { id: this.uid })
				if(res.meta.status !== 200) return uni.showToast({
					title: res.meta.message,
					icon: 'error',
					duration: 1000
				})
				this.addressList = res.recordset
			},
			// 当地址信息的删除图标被点击时触发  删除当前收获信息
			async delCurrentAddress(rid) {
				const { data: res } = await uni.$http.delete('/address/del', { id: this.uid, rid  })
				if(res.meta.status !== 200) return uni.showToast({
					title: res.meta.message,
					icon: 'error',
					duration: 1500
				})
				uni.showToast({
					title: res.meta.message,
					icon: 'success',
					duration: 1000
				})
				this.getUserAddressInfo()
			},
			// 添加新的收获地址信息被点击时触发
			gotoAddAddressInfo() {
				uni.navigateTo({
					url: '/subpages/newaddress/newaddress'
				})
			}
		}
	}
</script>

<style>
	.van-button--plain.van-button--primary {
		color: #4b0;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}
	
	.address-list-area {
		padding-bottom: 45px;
	}
</style>
